package nix.anim.sso.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONPObject;

import nix.anim.common.pojo.LogAnnotation;
import nix.anim.common.pojo.NixResult;
import nix.anim.sso.service.UserService;
import nix.anim.utils.CookieUtils;

/**
 * SSO用户处理Controller
 */
@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	@Value("${USER_SESSION_KEY}")
	private String USER_SESSION_KEY;
	
	@Value("${USER_SESSION_EXPIRE}")
	private Integer USER_SESSION_EXPIRE;
	
	@Value("${ROOT_MENU_ID}")
	private Integer ROOT_MENU_ID;
	
	@RequestMapping(value="/api/user/login", method=RequestMethod.POST)
	@ResponseBody
	@LogAnnotation
	public NixResult login(String username, String password, HttpServletRequest request, HttpServletResponse response) {
		// 调用服务
		NixResult result = userService.login(username, password);
		//if (result.getStatus() == 200) {
		//	// 写入Cookie
		//	CookieUtils.setCookie(request, response, USER_SESSION_KEY, result.getData().toString(), USER_SESSION_EXPIRE);
		//}
		return result;
	}
	
	@RequestMapping(value="/api/v1/user/token/{token}", method=RequestMethod.GET,
			// 指定返回响应数据的Content-Type
			produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object getUserByToken_V1(@PathVariable String token, String callback) {
		// 调用服务
		NixResult result = userService.getUserByToken(token);
		if (StringUtils.isNoneBlank(callback)) {
			JSONPObject jsonpObject = new JSONPObject(callback);
			jsonpObject.addParameter(result);
			return jsonpObject;
		}
		return result;
	}
	
	@RequestMapping(value="/api/user/token/{token}", method=RequestMethod.GET)
	@ResponseBody
	public NixResult getUserByToken(@PathVariable String token) {
		// 调用服务
		NixResult result = userService.getUserByToken(token);
		return result;
	}
	
	@RequestMapping(value="/api/v1/user/logout/{token}", method=RequestMethod.GET,
			// 指定返回响应数据的Content-Type
			produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object logout_V1(@PathVariable String token, String callback) {
		// 调用服务
		NixResult result = userService.logout(token);
		if (StringUtils.isNoneBlank(callback)) {
			JSONPObject jsonpObject = new JSONPObject(callback);
			jsonpObject.addParameter(result);
			return jsonpObject;
		}
		return result;
	}
	
	@RequestMapping(value="/api/user/logout/{token}", method=RequestMethod.GET)
	@ResponseBody
	public NixResult logout(@PathVariable String token) {
		// 调用服务
		NixResult result = userService.logout(token);
		return result;
	}
	
	@RequestMapping(value="/api/v1/user/changepwd/{token}", method=RequestMethod.GET,
			produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object changePassword_V1(@PathVariable String token, String oldPassword, String newPassword, String callback) {
		// 调用服务
		NixResult result = userService.changePassword(token, oldPassword, newPassword);
		if (StringUtils.isNoneBlank(callback)) {
			JSONPObject jsonpObject = new JSONPObject(callback);
			jsonpObject.addParameter(result);
			return jsonpObject;
		}
		return result;
	}
	
	@RequestMapping(value="/api/user/changepwd/{token}", method=RequestMethod.POST)
	@ResponseBody
	public NixResult changePassword(@PathVariable String token, String oldPassword, String newPassword) {
		// 调用服务
		NixResult result = userService.changePassword(token, oldPassword, newPassword);
		return result;
	}

	@RequestMapping(value="/api/v1/user/menu/{token}", method=RequestMethod.GET,
			produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object getUserMenuTree_V1(@PathVariable String token, String callback) {
		// 调用服务		
		NixResult result = userService.getUserMenuTree(ROOT_MENU_ID, token);
		if (StringUtils.isNoneBlank(callback)) {
			JSONPObject jsonpObject = new JSONPObject(callback);
			jsonpObject.addParameter(result);
			return jsonpObject;
		}
		return result;
	}

	@RequestMapping(value="/api/user/menu/{token}", method=RequestMethod.GET)
	@ResponseBody
	public NixResult getUserMenuTree(@PathVariable String token) {	
		// 调用服务		
		NixResult result = userService.getUserMenuTree(ROOT_MENU_ID, token);
		return result;
	}
}
